package ms.unicorn.palette;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ms.unicorn.diagram.providers.UnicornElementTypes;

import org.eclipse.gef.Request;
import org.eclipse.gef.Tool;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gmf.runtime.diagram.ui.tools.UnspecifiedTypeCreationTool;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;

public class NodeToolEntry extends ToolEntry {
	
	private String semanticHint;
	private List<IElementType> types;

	public NodeToolEntry(String label, String tooltip,
			String semanticHint) {		
		super(label, tooltip, null, null);
		this.semanticHint = semanticHint;
	}
	
	public NodeToolEntry(String label, String tooltip,
			ImageDescriptor smallImage, ImageDescriptor largeImage,
			String semanticHint,
			List<IElementType> elementTypes) {
		super(label, tooltip, smallImage, largeImage);
		this.semanticHint = semanticHint;
		this.types = elementTypes;
	}
	
	private List<IElementType> getDefaultElementTypes() {
		List<IElementType> types = new ArrayList<IElementType>();
		types.add(UnicornElementTypes.Entity_2001);
		//types.add(ElementTypeEx.wrap(UnicornElementTypes.Entity_2001, semanticHint, this.getLabel()));
		return types;
	}

	public Tool createTool() {
		//types = null;
		Tool tool = new TestEntityCreationTool(types != null ? types : getDefaultElementTypes());
		setToolProperty("hint", semanticHint);
		tool.setProperties(getToolProperties());
		return tool;
	}
	
	public static class TestEntityCreationTool extends UnspecifiedTypeCreationTool {
		
		@SuppressWarnings("rawtypes")
		private Map properties;

		public TestEntityCreationTool(@SuppressWarnings("rawtypes") List elementTypes) {
			super(elementTypes);
		}
		
		@Override
		public void setProperties(@SuppressWarnings("rawtypes") Map properties) {
			this.properties = properties;
		}
		
		@Override
		protected Request createTargetRequest() {
			Request request = super.createTargetRequest();
			request.setExtendedData(properties);
			return request;
		}
	}

}
